<?php

namespace App\Services\Tools;

/**
 * 即构控制语音流
 * Class ZegoService
 *
 * @package App\Services\Tools
 * anthor Hso
 */
class ZegoService
{
    public $host;
    public $commonData;
    public $secret;

    public function __construct()
    {
        $this->host = 'https://rtc-api.zego.im/';
        $this->commonData['AppId'] =  env('ZEGO_APPID');
        $this->secret = env('ZEGO_SERVER_SECRET');
        $this->commonData['SignatureVersion'] = '2.0';
        $this->commonData['IsTest'] = !empty(env('ZEGO_TEST')) ? 'true' : 'false';
    }

    // 生成签名
    protected function createSign($appId, $signatureNonce, $serverSecret, $timestamp)
    {
        $str = $appId.$signatureNonce.$serverSecret.$timestamp;
        $signature = md5($str);
        return $signature;
    }
    //简易流信息
    public function getSimpleStreamList($roomId,$curlHost,$curlMaxTime){
        $data = [
            'AppId' => $this->commonData['AppId'],
            'IsTest' => $this->commonData['IsTest'],
            'SignatureVersion' => $this->commonData['SignatureVersion'],
        ];
        $data['SignatureNonce'] =  bin2hex(random_bytes(8)); //生成16进制随机字符串(16位)
        $data['Timestamp'] = time(); // 时间戳
        $data['RoomId'] = $roomId;
        $data['Signature'] = $this->createSign($data['AppId'], $data['SignatureNonce'], $this->secret, $data['Timestamp']);
        foreach ($data as $k=> $v){
            $curlHost .= "&$k=".$v;
        }
        $res = $this->get($curlHost,$curlMaxTime);
        if(!isset($res['code'])){
            return [
                'code' => 4000,
                'data' => [
                    'msg' => 'curl返回空'
                ]
            ];
        }
        if( $res['code'] == 0){
            return [
                'code' => 200,
                'data' => $res
            ];
        }else{
            return [
                'code' => 400,
                'data' => $res
            ];
        }
    }

    public function get($url,$curlMaxTime)
    {
        //初始化
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curlMaxTime);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //处理http证书问题
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        //返回结果
        if (false === $output) {
            $res = [
                'code'=>400,
                'data'=>[
                    'errno' => curl_errno($ch),
                    'error' => curl_error($ch)
                ]
            ];
            return $res;
        }
        $result = json_decode($output, true);
        if(empty($result)){
            return  [];
        }
        $result['code'] = 0;
        return  $result;
    }


    // curl公用数据
    public function getCurlCommonData()
    {
        $data = [
            'AppId' => $this->commonData['AppId'],
            'IsTest' => $this->commonData['IsTest'],
            'SignatureVersion' => $this->commonData['SignatureVersion'],
        ];
        $data['SignatureNonce'] = bin2hex(random_bytes(8)); //生成16进制随机字符串(16位)
        $data['Timestamp'] = time(); // 时间戳
        $data['Signature'] = $this->createSign($data['AppId'], $data['SignatureNonce'], $this->secret, $data['Timestamp']);
        return $data;
    }


}
